package com.sskj.mine.ui.activity;


import android.Manifest;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.QRCodeUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.RechargeInfoBean;
import com.sskj.mine.presenter.RechargeActivityPresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;

@Route(path = RConfig.MINE_RECHARGE)//充值
public class RechargeActivity extends BaseActivity<RechargeActivityPresenter> {

    @BindView(R2.id.ivQRCode)
    ImageView ivQRCode;
    @BindView(R2.id.tvSave)
    TextView tvSave;
    @BindView(R2.id.tvCode)
    TextView tvCode;
    @BindView(R2.id.tvCopy)
    TextView tvCopy;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @Autowired
    String type;
    @Autowired
    String code;
    @Autowired
    String id;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_recharge;
    }

    @Override
    public RechargeActivityPresenter getPresenter() {
        return new RechargeActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mineRechargeActivity0));
        setRightImg(R.mipmap.mine_icon_recharge_record, v -> {
            ARouter.getInstance().build(RConfig.MINE_ASSERT_RECORD)
                    .withString(Constans.ID, id)
                    .withString(Constans.CODE, code)
                    .navigation();

        });
        tvDesc.setText(String.format(App.INSTANCE.getString(R.string.mineRechargeActivity3),
                code));
        mPresenter.recharge(code, type);

    }


    public void updateView(RechargeInfoBean bean) {
        tvCode.setText(bean.getAddr());
        if (TextUtils.isEmpty(bean.getAddr())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.mineRechargeActivity4));
            return;
        }
        QRCodeUtil.createImage(bean.getAddr(), (int) RudenessScreenHelper.pt2px(this, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                ivQRCode.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

            }
        });
        ClickUtil.click(tvCopy, () -> {
            CopyUtil.copy(tvCode.getText().toString());
        });
        ClickUtil.click(tvSave, () -> {
            new RxPermissions(RechargeActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            ImgUtil.saveImageToGallery(this, ivQRCode);

                        } else {
                            PermissionTipUtil.tip(RechargeActivity.this, App.INSTANCE.getString(R.string.mineRechargeActivity5));
                        }
                    });

        });

    }

}
